package com.cpx.config;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @Auther: PanBin
 * @Date: 2018-11-16 13:53
 * @Description: 定制的权限过滤器
 * @Version: 1.0
 */
public class CustomPermsAuthorizationFilter extends AuthorizationFilter {

    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        //获取主题
        Subject subject = this.getSubject(request, response);
        //获取到访问连接配置文件中的权限列表
        String[] perms = (String[]) mappedValue;
        if (perms == null || perms.length == 0) {
            //放行
            return true;
        }
        for (String p : perms) {
            if (subject.isPermitted(p)) {
                //只要一个符合就放行
                return true;
            }
        }
        return false;
    }
}
